package com.example.demo.config.logInterceptor;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

/**
 * @author shenb
 * @date 2022-07-15 14:05
 */
@Aspect
@Configuration
public class LogAspect {
    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("@annotation(com.example.demo.config.logInterceptor.Log)")
    public void pointcut() {
    }

    ;

    @Around("pointcut() && @annotation(log)")
    public Object around(ProceedingJoinPoint joinPoint, Log log) throws Throwable {
        long beginTime = System.currentTimeMillis();
        try {
            Object result = joinPoint.proceed();
            return result;
        } catch (Throwable e) {
            throw e;
        } finally {
            long cost = System.currentTimeMillis() - beginTime;
            logger.info("AspectLog [{}], 耗时: {}, : {}", log.level(), cost, log.value());
        }
    }

}
